package com.example.blog.util.UserUtils;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.blog.mapper.UserMapper;
import com.example.blog.pojo.User;
import com.xiaoleilu.hutool.json.JSONUtil;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
@Component
public class UserUtils {
    @Resource
    private UserMapper userMapper;
    //获取用户id
    public  Integer getUserId(String userName){
        QueryWrapper<User> Wrapper = new QueryWrapper<>();
        QueryWrapper<User> eq = Wrapper.eq("username",userName);
        List<User> list = userMapper.selectList(eq);
        String json = JSONUtil.toJsonStr(list);
        User u = JSONObject.parseObject(json, User.class);
        return u.getId();
    }
    //获取用户昵称
    public  String getNickname(String userName){
        QueryWrapper<User> Wrapper = new QueryWrapper<>();
        QueryWrapper<User> eq = Wrapper.eq("username",userName);
        List<User> list = userMapper.selectList(eq);
        String json = JSONUtil.toJsonStr(list);
        User u = JSONObject.parseObject(json, User.class);
        return u.getNickname();
    }
    //获取用户头像
    public  String getHeaderUrl(String userName){
        QueryWrapper<User> Wrapper = new QueryWrapper<>();
        QueryWrapper<User> eq = Wrapper.eq("username",userName);
        List<User> list = userMapper.selectList(eq);
        String json = JSONUtil.toJsonStr(list);
        User u = JSONObject.parseObject(json, User.class);
        return u.getHeaderurl();
    }
    //获取用户身份
    public  int getIdentity(String userName){
        QueryWrapper<User> Wrapper = new QueryWrapper<>();
        QueryWrapper<User> eq = Wrapper.eq("username",userName);
        List<User> list = userMapper.selectList(eq);
        String json = JSONUtil.toJsonStr(list);
        User u = JSONObject.parseObject(json, User.class);
        return u.getIdentity();
    }
    public  String getName(Integer id){
        User user = userMapper.selectById(id);
        return user.getNickname();
    }

    public  String getUrl(Integer id){
        User user = userMapper.selectById(id);
        return user.getHeaderurl();
    }

}
